#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include <SFML/OpenGL.hpp>

#include "TextBox.h"
//#include "IGameState.h"
#include "StateIntroScreen.h"
#include "KeyLock.h"
#include "animSprite.h"
#include "SpriteSheetManager.h"
//#include "testUnit.h"
#include "FrameRateManger.h"
#include "Terrain.h"

#include "UnitFactory.h"

#include "AL_IAbility.h"
//#include "Pathfinding.h"

namespace game
{


class Game
{
	//declare psuedo - singleton managers//
	sf::RenderWindow*			_window;
	game::KeyLock*				_inputManager;
	game::GameStateManager*		_gameStateManager; //
	game::SpriteSheetManager*	_spriteSheetManager;
	game::FrameRateManager*		_frameRateManager;//
	game::TerrainManager*		_terrain_manager;
	game::Camera*				_camera;
	game::UnitFactory*			_unit_factory;

public:
	Game()
	{
		//Init Factories
		
		//iniaialise managers//
		_window = new sf::RenderWindow(sf::VideoMode(640, 640), "SFML works!"/*, sf::Style::Fullscreen*/);
		_inputManager = new game::KeyLock(_window);
		_spriteSheetManager = new game::SpriteSheetManager();
		_camera = new Camera(sf::Vector2f(0,0),sf::Vector2u(640,640));
		_terrain_manager = new game::TerrainManager(_window,_camera,_spriteSheetManager);
		_unit_factory = new UnitFactory(_spriteSheetManager,_window,_terrain_manager);
		
		

		_gameStateManager = new game::GameStateManager(	_window,
														_inputManager,
														_spriteSheetManager,
														_terrain_manager,
														_unit_factory);

		_frameRateManager = new game::FrameRateManager(60);//

		//load managers//

		int t_ids[] = {6,1,14,16,18};_terrain_manager->LoadTerrainSprites(0,t_ids,5);
		_gameStateManager->AddState(new game::StateIntroScreen2(_gameStateManager));	//menu screen//
		_spriteSheetManager->LoadSheet("img\\sb1.png",32,6,0);
		_spriteSheetManager->LoadSheet("img\\alpha_6x1.png",32,6,1);
		_spriteSheetManager->LoadSheet("img\\apritesheet1.png",32,4,2);

		//Load Factories//
		_unit_factory->LoadFactory();



	}
	~Game()
	{
		//Delete all pointers to managers//
		if (_window)
		delete _window;			std::cout << "Deleted Window" << std::endl;
		if (_inputManager)
		delete _inputManager;	std::cout << "Deleted InputManager" << std::endl;
		if (_gameStateManager)//
		delete _gameStateManager;//
		if (_spriteSheetManager)
		delete _spriteSheetManager; std::cout << "Deleted SpriteSheetManager" << std::endl;
		if (_frameRateManager)//
		delete _frameRateManager;std::cout << "Deleted FrameRateManager" << std::endl;
		if (_terrain_manager)
		delete _terrain_manager;
	}
	sf::RenderWindow * getWindow(){return _window;}
	void Draw()	const
	{
		_window->clear();
		_gameStateManager->Draw();
		_window->display();
	}
	void Update()	
	{
		sf::Event event;
		bool exiting_game = false;
        while (_window->pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
			{
				exiting_game = true;
                _window->close();
			}
        }
		_gameStateManager->Update();
		_frameRateManager->Wait();
	}

};

}